package com.zym.domain;

import com.alibaba.fastjson.JSON;
import lombok.Data;

/**
* @program AjaxRequest
* @description 自定义响应结构
* @author Mr.Zou
* @create 2022-01-04 22:19
*/
@Data
public class AjaxRequest {
    /**
    * @program AjaxRequest
    * @description 响应状态
    * @author Mr.Zou
    * @create 2022-01-04 22:22
    */
    private Integer code;
    /**
    * @program AjaxRequest
    * @description 响应消息
    * @author Mr.Zou
    * @create 2022-01-04 22:23
    */
    private String message;
    /**
    * @program AjaxRequest
    * @description 响应中的数据
    * @author Mr.Zou
    * @create 2022-01-04 22:23
    */
    private Object data;

    public AjaxRequest(){}

    public AjaxRequest(Object data) {
        this.data = data;
    }

    public AjaxRequest(String message, Object data) {
        this.message = message;
        this.data = data;
    }

    public AjaxRequest(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static AjaxRequest ok() {
        return new AjaxRequest(null);
    }
    public static AjaxRequest ok(String message) {
        return new AjaxRequest(message, null);
    }
    public static AjaxRequest ok(Object data) {
        return new AjaxRequest(data);
    }
    public static AjaxRequest ok(String message, Object data) {
        return new AjaxRequest(message, data);
    }

    public static AjaxRequest build(Integer code, String message) {
        return new AjaxRequest(code, message, null);
    }

    public static AjaxRequest build(Integer code, String message, Object data) {
        return new AjaxRequest(code, message, data);
    }

    public String toJsonString() {
        return JSON.toJSONString(this);
    }


    /**
     * JSON字符串转成 Result 对象
     * @param json
     * @return
     */
    public static AjaxRequest format(String json) {
        try {
            return JSON.parseObject(json, AjaxRequest.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
